/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.pima.gestrest.converter;

import com.pima.gestrest.controller.CadreController;
import com.pima.gestrest.model.Cadre;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.model.SelectItem;

/**
 *
 * @author Pierre-Louis Conte
 */
@FacesConverter("cadreConverter")
public class CadreConverter implements Converter {

    @Override
    public Cadre getAsObject(FacesContext fc, UIComponent uic, String string) {
       //  throw new UnsupportedOperationException("Not supported yet.");

        String [] str_tab;
        str_tab = string.split(", ");

        CadreController cadreController = (CadreController)fc.getApplication().getELResolver().getValue(fc.getELContext(), null, "cadreController");
        for(SelectItem item : cadreController.getItemList())
        {
            Cadre cadre = (Cadre)item.getValue();
            if(cadre.getId().toString().equals(str_tab[0]) && cadre.getTitle().equals(str_tab[1]) )
                return (Cadre)item.getValue();
        }

        return null;
    }

    @Override
    public String getAsString(FacesContext fc, UIComponent uic, Object o) {
      //  throw new UnsupportedOperationException("Not supported yet.");
      return ((Cadre)o).getId()+", "+((Cadre)o).getTitle() ;
    }

}
